package com.wxservice.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.wxservice.domain.request.TagREQ;
import com.wxservice.domain.request.UserTagREQ;
import com.wxservice.domain.request.WXUserREQ;
import com.wxservice.service.WXUserManageService;
import com.wxservice.util.CommonUtil;
import com.wxservice.util.ErrorUtil;
import org.springframework.stereotype.Service;

@Service
public class WXUserManageServiceImpl implements WXUserManageService {


    @Override
    public String listUserOpenId(WXUserREQ wxUserREQ) {
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", wxUserREQ.getAccess_token());
        if (wxUserREQ.getNext_openid() != null && !("".equals(wxUserREQ.getNext_openid()))) {
            requestUrl += ("&next_openid=" + wxUserREQ.getNext_openid());
        }
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String getUserInfo(WXUserREQ wxUserREQ) {
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", wxUserREQ.getAccess_token());
        requestUrl = requestUrl.replace("OPENID", wxUserREQ.getOpenid());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String listUserInfo(WXUserREQ wxUserREQ) {
        String jsonString = JSONObject.toJSONString(wxUserREQ);
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", wxUserREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String insertTag(TagREQ tagREQ) {
        String jsonString = JSONObject.toJSONString(tagREQ);
        jsonString = "{\"tag\":" + jsonString + "}";
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", tagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String listTag(String access_token) {
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", access_token);
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String updateTag(TagREQ tagREQ) {
        String jsonString = JSONObject.toJSONString(tagREQ);
        jsonString = "{\"tag\":" + jsonString + "}";
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", tagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String deleteTag(TagREQ tagREQ) {
        String jsonString = JSONObject.toJSONString(tagREQ);
        jsonString = "{\"tag\":" + jsonString + "}";
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", tagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String listUserByTag(UserTagREQ userTagREQ) {
        String jsonString = JSONObject.toJSONString(userTagREQ);
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", userTagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String addUsersToTag(UserTagREQ userTagREQ) {
        String jsonString = JSONObject.toJSONString(userTagREQ);
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", userTagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String deleteUsersToTag(UserTagREQ userTagREQ) {
        String jsonString = JSONObject.toJSONString(userTagREQ);
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", userTagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String listTagsByUser(UserTagREQ userTagREQ) {
        String jsonString = JSONObject.toJSONString(userTagREQ);
        String result = "failure";
        // 微信接口
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", userTagREQ.getAccess_token());
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        } else if (null != jsonObject && jsonObject.containsKey("errcode")) {
            result = ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }
}
